import { reactive } from "vue";
import { message } from "ant-design-vue";
import {insertElectricUpload} from "@/api/article"
import moment from 'moment';
import {formatDate} from "@/hooks/index"

const history = (): any => {
  /**
   * 声明表格数据
   */

  /**
   * 声明抽屉数据
   */
  const drawerState=reactive({
    visible:false,
    title:"新增摄像头",
    quantity:100,
    form:{
      aid:4000012,//桩Id	
      userID:32350,//用户id
      quantity:140.86,
    },
    value1:0,
    switch:true,//代表新增
    ruleForm:"ruleForm",//表单ref
    
    labelCol: { span: 4 },
    wrapperCol: { span: 14 },
  })

  /**
   * 提交表单
   */
  function onSubmit() {
    // (drawerState.ruleForm as any).validate()
    // .then(() => {
    //   // 转化时间戳
      
    //   console.log('values', drawerState.form);
    // })
    // .catch((error:any) => {
      
      
    // });
      let obj={
        aid:4000012,//桩Id	
        userID:32350,//用户id
        startTime:0,//开始时间
        endTime:0,//结束时间
        token:"yZdTxtMhwIJHOg10lQFI",
        clientPortID:1,
        bussinessType:1,
        transactionType:0, 
        serialNumber:'20/12/01/02:48:39',//开始时间
        totalStart:0,
        totalEnd:0,//用电量 x 100	
        peakPrice:1,
        peakValue:0,
        peakAmount:0,
        flatPrice:1,
        flatValue:100,//用电量 x 100	
        flatAmount:100,//用电量 x 100	
        valleyPrice:1,
        valleyValue:0,
        valleyAmount:0,
        totalValue:100,////用电量 x 100	
        totalAmount:100,//用电量 x 100	
        servicePrice:0,
        serviceAmount:0,
        transactionAmount:100,//用电量 x 100	
        transactionTime:0//endTime
      }
    
        let st:number=(drawerState.value1 as any)[0]._d;
        let ed:number=(drawerState.value1 as any)[1]._d;
        console.log(Math.ceil(Number(st)/1000));
        console.log(Math.ceil(Number(ed)/1000));
        obj.startTime=Math.ceil(Number(st)/1000)
        obj.endTime=Math.ceil(Number(ed)/1000)
        //用电量*100
        obj.totalEnd=Number((drawerState.form.quantity*100).toFixed(2))
        obj.flatValue=Number((drawerState.form.quantity*100).toFixed(2))
        obj.flatAmount=Number((drawerState.form.quantity*100).toFixed(2))
        obj.totalValue=Number((drawerState.form.quantity*100).toFixed(2))
        obj.totalAmount=Number((drawerState.form.quantity*100).toFixed(2))
        obj.transactionAmount=Number((drawerState.form.quantity*100).toFixed(2))
        // 转化是字符串时间
        obj.serialNumber=formatDate(obj.startTime)
        obj.transactionTime=obj.endTime
        // id 转字符串
        // drawerState.form.aid=Number(drawerState.form.aid)
        // drawerState.form.userID=Number(drawerState.form.userID)
        obj.aid=drawerState.form.aid
        obj.userID=drawerState.form.userID
        insertElectricUpload((obj)).then(res=>{
          console.log(res)
          message.success("请求成功")
        })
        .catch(err=>{
          message.error("请求错误")
        })
      // }catch{
      //   message.error("请选择时间")
      // }
      // 转化用电量

  }
  /**
   * 编辑表单
   * @param record 
   */
    function edit(record:any){
  
     console.log(record)
      drawerState.visible=true
  }

  /**
   * 取消删除
   * @param e 
   */
  function cancel(e:any) {
    console.log(e);
    message.error('Click on No');
  }
  function range(start:any, end:any) {
    const result = [];
    for (let i = start; i < end; i++) {
      result.push(i);
    }
    return result;
  }
  function disabledDate(current:any) {
      // Can not select days before today and today
      return current && current < moment().endOf('day');
    }
  
  function disabledRangeTime(_:any, type:any) {
    if (type === 'start') {
      return {
        disabledHours: () => range(0, 60).splice(4, 20),
        disabledMinutes: () => range(30, 60),
        disabledSeconds: () => [55, 56],
      };
    }
  }
  return {drawerState,onSubmit,edit,cancel,range,disabledDate,disabledRangeTime}
}

export { history }
